Hi,
I'm using Phalcon as the application framework to use with a Ratchet WebSocket server. Note that Ratchet is a PHP server based on ReactPHP, so it uses the reactor pattern (single thread.)
On a new connection, I just get the uri and call $application->handle($uri). I do the same when I receive a message.
The problem is that when I receive a connection everything works as it is expected, but when I receive a message and I call $application->handle($uri), the server gets stuck. So I think that the problem is caused by the router.
Relevant code:
Phalcon application services (router):
$loader = new Phalcon\Loader();
$loader->registerDirs(
array(
'./Mvc/controllers/',
'./Mvc/models/'
)
)->register();
$di = new Phalcon\DI\FactoryDefault();
$di->set('router', function () {
$router = new Phalcon\Mvc\Router();
$router->setDefaultNamespace('Wings\WsServer\Mvc\Controllers');
$router->add(
"/raw/echo",
array(
"controller" => "Raw",
"action" => "echo",
)
);
$router->add(
'/GET/resource/ws/{conn_id:[0-9]+}',
array(
'controller' => 'connection',
'action' => 'new'
)
);
$router->add(
'/GET/resource/thing/query',
array(
'controller' => 'thing',
'action' => 'query'
)
);
return $router;
});
Connection Handler (observe the var_dump($conn->WebSocket->request->getPath()); and var_dump($jsonArray["uri"]);).
public function onOpen(ConnectionInterface $conn)
{
// Store the new connection to send messages to later
$this->clients->attach($conn);
echo 'Client connected on WS server!';
$application = $this->_di->getRaw('application');
$application->getDI()->set('connection', $conn);
var_dump($conn->WebSocket->request->getPath());
$content = $application->handle($conn->WebSocket->request->getPath())->getContent();
echo 'Client connection handled!';
$conn->send($content);
}
public function onMessage(ConnectionInterface $from, $msg)
{
$jsonArray = json_decode($msg, true);
$application = $this->_di->getRaw('application');
$application->getDI()->set('connection', $from);
$application->getDI()->set('data', $jsonArray["query"]);
var_dump($jsonArray["uri"]);
$content = $application->handle($jsonArray["uri"])->getContent();
$from->send($content);
}
What I get then on the shell is
Client connected on WS server!string(18) "/GET/resource/ws/1"
Client connection handled!string(25) "/GET/resource/thing/query"
and the server gets stuck. If a put an exit(-1); after
$content = $application->handle($jsonArray["uri"])->getContent();
the exit is never reached.
Any help would be appreciated.